package org.hentai.bt.util.sql;

import org.hentai.bt.DbConnectionMsg;
import org.hentai.bt.anno.ConnectDBMsg;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SqlConnection {

    private ConnectDBMsg msg;

    private SqlConnection(ConnectDBMsg msg) {
        this.msg = msg;
    }

    private Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(msg.driver());

        return DriverManager.getConnection(
                msg.type().getURL(msg.host(), msg.port(), msg.dbName()),
                msg.user(), msg.password()
        );

    }

    public static Connection getConn(ConnectDBMsg msg) throws SQLException, ClassNotFoundException {
        return new SqlConnection(msg).getConnection();
    }

    public static Connection getConn() throws SQLException, ClassNotFoundException {
        return SqlConnection.getConn(DbConnectionMsg.getDbMsg());
    }

}
